package com.zjw.zy.coreui.fragment.tips

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import android.widget.EditText
import android.widget.TextView
import android.widget.TimePicker
import androidx.fragment.app.viewModels
import com.zjw.zy.R
import com.zjw.zy.base.BaseFragment
import com.zjw.zy.base.ConflictScrollView
import com.zjw.zy.bean.Tip
import com.zjw.zy.coreui.view.CalendarItemView
import com.zjw.zy.coreui.view.CalendarView
import com.zjw.zy.coreui.view.ExpandView
import com.zjw.zy.ext.back
import com.zjw.zy.ext.mainActivity
import com.zjw.zy.ext.value
import com.zjw.zy.utils.TimeUtils
import com.zjw.zy.utils.WindowUtils
import com.zjw.zy.viewmodel.TipsViewModel

/**
 * @author     ：zhong.jw
 * @date       ：Created in 2022/11/2 17:19
 * @description：
 */
class TipEditFragment : BaseFragment() {

    companion object {
        private const val TAG = "TipEditFragment"

        const val DATA_KEY = "TipEditFragment_DATA_KEY"
    }

    private val tipViewModel: TipsViewModel by viewModels()

    private val mDateExpandView: ExpandView by lazy { findViewById(R.id.fragment_create_date) }
    private val mTimeExpandView: ExpandView by lazy { findViewById(R.id.fragment_create_time) }
    private val mTip: EditText by lazy { findViewById(R.id.fragment_create_tips) }
    private val mTag: AutoCompleteTextView by lazy { findViewById(R.id.fragment_create_tag) }
    private val mContent: ConflictScrollView by lazy { findViewById(R.id.fragment_edit_tips_content) }
    private val mCancel: TextView by lazy { findViewById(R.id.fragment_create_cancel) }
    private val mOk: TextView by lazy { findViewById(R.id.fragment_create_ok) }
    private val mTagAdapter: ArrayAdapter<String> by lazy {
        ArrayAdapter<String>(
            requireContext(),
            android.R.layout.simple_dropdown_item_1line
        )
    }

    private var mTipData: Tip? = null

    override fun onPageEnter() {
        WindowUtils.TopBar.hide()
        WindowUtils.FootBar.hide()
        mainActivity().setFullBackGround(R.color.transparent)

        tipViewModel.requireTags()
    }

    override fun onViewRestore(savedInstanceState: Bundle?) {

    }

    override fun onSubscribe() {
        tipViewModel.getTags().observe(this) {
            mTagAdapter.addAll(it)
        }
    }

    override fun onBindViewListener() {
        mCancel.setOnClickListener {
            back()
        }

        mOk.setOnClickListener {
            doOk()
        }

        mTag.setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                mTag.showDropDown()
            } else {
                mTag.dismissDropDown()
            }
        }

        mTip.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                mOk.isEnabled = !s.isNullOrEmpty()
            }

            override fun afterTextChanged(s: Editable?) {

            }
        })


        mDateExpandView.apply {
            this.addExpandView(CalendarView(context))
            this.getExpandView<CalendarView>().bindListener(object : CalendarView.Listener {
                override fun onItemClick(view: CalendarItemView) {
                    mDateExpandView.tag = view.getData()
                    mDateExpandView.setInfo(view.getData().stringOfYears)
                }
            })
            this.bindListener(object : ExpandView.Listener {
                override fun onExpandChange(expand: Boolean) {
                    if (!expand) {
                        mDateExpandView.tag = null
                        mDateExpandView.setInfo("")
                        mTimeExpandView.unExpand()
                    } else {
                        val now = TimeUtils.currentTime()
                        mDateExpandView.tag = now
                        mDateExpandView.setInfo(now.stringOfYears)
                        mDateExpandView.getExpandView<CalendarView>().bindData()
                    }
                }
            })
        }

        mTimeExpandView.apply {
            this.addExpandView(TimePicker(context))
            this.getExpandView<TimePicker>().let {
                it.setIs24HourView(true)
                it.setOnTimeChangedListener { view, hourOfDay, minute ->
                    val time = TimeUtils.getTimeBean(hourOfDay, minute, 0)
                    mTimeExpandView.tag = time
                    mTimeExpandView.setInfo(time.stringOfTime)
                }
                mContent.addScrollView(it)
            }
            this.bindListener(object : ExpandView.Listener {
                override fun onExpandChange(expand: Boolean) {
                    if (expand) {
                        val hour = mTimeExpandView.getExpandView<TimePicker>().hour
                        val minute = mTimeExpandView.getExpandView<TimePicker>().minute
                        val time = TimeUtils.getTimeBean(hour, minute, 0)
                        mTimeExpandView.tag = time
                        mTimeExpandView.setInfo(time.stringOfTime)

                        if (!mDateExpandView.isExpand()) {
                            mDateExpandView.expand()
                        }
                    } else {
                        mTimeExpandView.tag = null
                        mTimeExpandView.setInfo("")
                    }
                }
            })
        }

        mTipData?.let { tip ->
            mDateExpandView.getExpandView<CalendarView>().apply {
                mDateExpandView.expand()
                val date = TimeUtils.getTimeBean(tip.time)
                this.bindData(date.year, date.month)
                this.setSelectDay(date.day)
            }
            mTimeExpandView.getExpandView<TimePicker>().apply {
                mTimeExpandView.expand()
                val date = TimeUtils.getTimeBean(tip.time)
                this.hour = date.hour
                this.minute = date.minute
            }
        }
    }

    private fun doOk(isEdit: Boolean = mTipData != null) {
        val tip = mTip.value()
        var time = 0L
        (mDateExpandView.tag as? TimeUtils.TimeBean)?.let { date ->
            (mTimeExpandView.tag as? TimeUtils.TimeBean)?.let { time ->
                date.updateTime(time.hour, time.minute, time.second)
            }

            time = date.currentTime
        }
        val tag = mTag.value()
        if (!isEdit) {
            Tip(0, tip, time, false, tag).apply {
                tipViewModel.insert(this)
            }
        } else {
            mTipData?.let {
                it.tag = tag
                it.tip = tip
                it.time = time
                tipViewModel.updateItem(it)
            }
        }
        back()
    }

    override fun onBindViewValue() {
        mTag.setAdapter(mTagAdapter)

        arguments?.getParcelable<Tip>(DATA_KEY)?.let {
            mTipData = it.copy()
            mTip.setText(mTipData!!.tip)
            mTag.setText(mTipData!!.tag)
        }


    }

    override fun onFindView(rootView: View?) {

    }

    override fun onLayoutId(): Int {
        return R.layout.fragment_edit_tips
    }

    override fun onPageExit() {

    }
}